


//ejs 视图
import loginView from "$v/login"
import { loginRequest } from "../api/admin"


export default (req, res, next) => { //哈希路由 访问xxx/#/path
  // const { params, query, body , url, route } = req

  //放入视图
  res.render(loginView())

  //点击登录按钮逻辑
  document.loginForm.loginBtn.onclick = async (e) => {
    e.preventDefault()
    let adminNameValue = document.loginForm.adminName.value
    let passWordValue = document.loginForm.passWord.value

    try {
      const loginResult = await loginRequest({ adminName: adminNameValue, passWord: passWordValue })
      // console.log(loginResult);


      //登录成功可以拿到token,把token保存在localStorage中,并跳转到首页
      localStorage.setItem("user_token", loginResult.token)

      router.go("/adv/adminList")



    } catch (e) {
      alert("登陆异常" + e.message)
    }
  }

}